package com.cskaoyan._02method._02test;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Macro_Ray
 * @since 2024/3/5 15:28
 */
/*
 * 拷贝数组
 * 定义一个方法copyOfRange(int[] arr,int from, int to)
 * 将数组arr中从索引from(包含from)开始，到索引to结束(不包含to)的元素复制到新数组中，将新数组返回
 */
public class _05Overload_04 {
    public static void main(String[] args) {
        int[] arr1 = {1, 55, 22, 3, 156, 62, 856, 21, 5, 22, 426, 69, 2, 788, 4};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入索引开始：");
        int from = sc.nextInt();
        System.out.println("请输入索引结束：");
        int to = sc.nextInt();
        // 判断输入的索引是否在数组范围内
        if (from >= 0 && to < arr1.length) {
            System.out.print(Arrays.toString(copyOfRange(arr1, from, to)));
        } else {
            System.out.println("错误，索引越界！");
        }
    }
    // 复制的方法
    public static int[] copyOfRange(int[] arr, int from, int to) {
        int[] newArr = new int[to - from];

        for (int i = from, j = 0; i < to; i++, j++) {
            newArr[j] = arr[i];
        }
        return newArr;
    }
}
